﻿using System;
using Com.PhilChuang.Pmvp.Model.Services;
using Com.PhilChuang.Pmvp.Ui.Views;
using Com.PhilChuang.Utils;

namespace Com.PhilChuang.Pmvp.Ui.Presenters
{
	// TODO refactor by subclassing from SendFeedbackPresenter
	public class ReportBugPresenter : AbstractEmailSendingPresenter<IReportBugView>
	{
		#region Constants
		private const String SAVE_FILE = "ReportBug.txt";
		#endregion

		#region Instance variables
		private readonly IExceptionLogService m_ExceptionFileCheckService;
		private readonly ReportBugPresenterConfig m_Config;
		private String m_BugText;
		#endregion

		#region Constructors
		public ReportBugPresenter () : this (
			AppServiceLocator.Instance.GetImpl<IExceptionLogService> (), 
			AppServiceLocator.Instance.GetImpl<ReportBugPresenterConfig> (), 
			AppServiceLocator.Instance.GetImpl<IReportBugView> ())
		{
		}

		public ReportBugPresenter (IReportBugView view) : this (
			AppServiceLocator.Instance.GetImpl<IExceptionLogService> (), 
			AppServiceLocator.Instance.GetImpl<ReportBugPresenterConfig> (), 
			view)
		{
		}

		public ReportBugPresenter (IExceptionLogService service, ReportBugPresenterConfig config, IReportBugView view) : base (view)
		{
			service.ThrowIfNull ("service");
			config.ThrowIfNull ("m_Config");
			
			m_ExceptionFileCheckService = service;
			m_Config = config;
		}
		#endregion

		#region AbstractPresenter implementation
		/// <summary>
		/// Refreshes Presenter data from the Application.
		/// </summary>
		protected override void RefreshData ()
		{
			if (m_ExceptionFileCheckService.HasExceptionLog)
				m_BugText = m_ExceptionFileCheckService.GetExceptionLog ();
		}

		///// <summary>
		///// Updates the View with Presenter data.
		///// Call base impl as the last step.
		///// </summary>
		//protected override void UpdateToView ()
		//{
		//    View.BugText = m_BugText;
		//    base.UpdateToView ();
		//}

		public override void Show ()
		{
			View.IsBugReport = true;
			if (View.DisplayYesNoChoice ("Looks like an error occurred the last time you ran this app!  Would you like to report the error?", "Sorry!"))
			{
				base.Show ();
			}
			else
			{
				m_ExceptionFileCheckService.DeleteExceptionLog ();
				View.Close ();
			}
		}
		#endregion

		#region AbstractEmailSendingPresenter implementation
		protected override string Prompt { get { return "What were you doing when the application crashed or errored?"; } }

		protected override bool Validate ()
		{
			if (!base.Validate ())
				return false;

			if (m_UserText.IsNullOrEmptyOrBlank ())
			{
				View.DisplayMessage ("Please enter some details about the crash/error.", "Error");
				return false;
			}

			return true;
		}

		protected override String EmailDestination { get { return m_Config.FeedbackEmailAddress; } }
		protected override String EmailSubject { get { return m_Config.FeedbackEmailSubject; } }
		protected override string EmailBody { get { return m_Config.FeedbackEmailBodyFormat.FormatWith (m_UserText, m_BugText); } }

		protected override string SaveFilename { get { return SAVE_FILE; } }

		protected override void OnDoSendEmail ()
		{
			base.OnDoSendEmail ();
			m_ExceptionFileCheckService.DeleteExceptionLog ();
		}
		#endregion
	}
}
